function login(){}
login.prototype = {
    createSession: function () {
        document.getElementById('notAllowedQuestionForm').remove();
        this.createLoginForm()
    },
    sendForm: function () {
        let password            = document.getElementById('password').value;
        let newPassword         = document.getElementById('newPassword').value ;
        let confimNewPassword   = document.getElementById('confirmNewPassword').value;
        let isValid1 = this.validateNewPassword(newPassword, confimNewPassword);
        let isValid2 = this.validateOldAndNewPassword(password, newPassword);
        if (isValid1['status'] && isValid2['status']) {
            this.loginUser(password, newPassword);
            return;
        }
        let message = '';
        if (!isValid1.status) {
            message = isValid1.message + '
';
        }
        if (!isValid2.status) {
            message += isValid2.message;
        }
        let modalError = new Modal({
            title: 'Aviso',
            body : '
' + message + '
',
            buttonOK : false,
            cancelText: 'OK',
        });
        modalError.show();
    },
    validateOldAndNewPassword: function (oldPassword, newPassword) {
        let result = {'status': false, 'message': translate['old-new-password-must-be-different']};
        if (oldPassword == newPassword) {
            return result;
        }
        result = {'status': true};
        return result;
    },
    validateNewPassword: function (newPassword, confirmNewPassword) {
        let result = {'status': false, 'message': translate['new-password-must-be-equals']};
        if (newPassword !== confirmNewPassword) {
            return result;
        }
        result = {'status': true};
        return result;
    },
    loginUser: function (password, newPassword) {
        ajaxLoader.ajaxStart();
        $.ajax({
            url: '/login/login-new-session',
            data: {'password': password, 'newPassword': newPassword},
            type: 'post',
            dataType: 'json',
            success: function(json) {
                ajaxLoader.ajaxStop();
                if(json.status) {
                    window.location.href = '/';
                } else {
                    let modalError = new Modal({
                        title: 'Aviso',
                        body : ''+json.message+'
',
                        buttonOK : false,
                        cancelText: 'OK',
                    });
                    modalError.show();
                }
            },
        });
    },
    loginUserNoChange: function() {
        $.ajax({
            url: '/login/login-new-session-no-change',
            data: {},
            type: 'post',
            dataType: 'json',
            success: function(json) {
                ajaxLoader.ajaxStop();
                if(json.status) {
                    window.location.href = '/';
                } else {
                    let modalError = new Modal({
                        title: 'Aviso',
                        body : ''+json.message+'
',
                        buttonOK : false,
                        cancelText: 'OK',
                    });
                    modalError.show();
                }
            },
        });
    },
    createLoginForm: function () {
        let divContainerLoginForm = document.createElement('div');
        let boxSize = document.getElementById('size-box');
        boxSize.setAttribute('class', 'col-md-6 center paddingMobile0');
        let divBoxFormCinza = document.createElement('div');
        divBoxFormCinza.setAttribute('class', 'box-form-cinza');
        let divFormGroupPassword = document.createElement('div');
        divFormGroupPassword.setAttribute('class', 'form-group');
        let divFormGroupNewPassword = document.createElement('div');
        divFormGroupNewPassword.setAttribute('class', 'form-group');
        let divFormGroupConfirmNewPassword = document.createElement('div');
        divFormGroupConfirmNewPassword.setAttribute('class', 'form-group');
        let labelPassword = document.createElement('label');
        labelPassword.innerHTML = translate['password'];
        let labelNewPassword = document.createElement('label');
        labelNewPassword.innerHTML = translate['new-password'];
        let labelCofirmNewPassword = document.createElement('label');
        labelCofirmNewPassword.innerHTML = translate['confirm-new-password'];
        let inputPassword = document.createElement('input');
        inputPassword.setAttribute('type', 'password');
        inputPassword.setAttribute('id', 'password');
        inputPassword.setAttribute('class', 'form-control');
        let inputNewPassword = document.createElement('input');
        inputNewPassword.setAttribute('type', 'password');
        inputNewPassword.setAttribute('id', 'newPassword');
        inputNewPassword.setAttribute('class', 'form-control');
        let inputConfirmNewPassoword = document.createElement('input');
        inputConfirmNewPassoword.setAttribute('type', 'password');
        inputConfirmNewPassoword.setAttribute('id', 'confirmNewPassword');
        inputConfirmNewPassoword.setAttribute('class', 'form-control');
        inputConfirmNewPassoword.setAttribute('placeholeder', 'teste');
        let submitButton = document.createElement('button');
        submitButton.setAttribute('onclick', 'login.sendForm()');
        submitButton.setAttribute('class', 'btn-padrao-ativo col-xs-12 cadastro');
        submitButton.innerText = translate['change-password-access'];
        let backButton = document.createElement('button');
        backButton.setAttribute('onclick', 'login.notCreateSession()');
        backButton.setAttribute('class', 'btn-padrao-ativo col-xs-12 cadastro');
        backButton.innerText = translate['back'];
        divFormGroupPassword.appendChild(labelPassword);
        divFormGroupPassword.appendChild(inputPassword);
        divBoxFormCinza.appendChild(divFormGroupPassword);
        divFormGroupNewPassword.appendChild(labelNewPassword);
        divFormGroupNewPassword.appendChild(inputNewPassword);
        divBoxFormCinza.appendChild(divFormGroupNewPassword);
        divFormGroupConfirmNewPassword.appendChild(labelCofirmNewPassword);
        divFormGroupConfirmNewPassword.appendChild(inputConfirmNewPassoword);
        divBoxFormCinza.appendChild(divFormGroupConfirmNewPassword);
        divBoxFormCinza.appendChild(submitButton);
        divBoxFormCinza.appendChild(backButton);
        divContainerLoginForm.appendChild(divBoxFormCinza);
        document.getElementById('container').appendChild(divContainerLoginForm);
    },
    notCreateSession: function() {
        $.ajax({
            url: '/login/clear-not-logged',
            type: 'get',
            dataType: 'json',
            success: function(json) {
                window.location.href = '/';
            },
        });
    },
    validateSession: function () {
        // Ativando o ESC key antes de abrir a MODAL (Fechar o fullscreen)
        var esc = $.Event("keydown", { keyCode: 27 });
        $("body").trigger(esc);
        $.ajax({
            url: '/login/validate-session',
            type: 'get',
            dataType: 'json',
            success: function(json) {
                if (!json.status) {
                    var modal = new Modal({
                        dismissButton: false,
                        title: json.title,
                        body : ''+json.message+'
',
                        buttonOK : false,
                        buttonCancel: false,
                    });
                    modal.show();
                    window.setInterval( function() {
                        window.location = '/logout';
                    }, 3000)
                } else {
                    return;
                }
            },
        });
    },
    getUserTokens: function() {
        
        $.ajax({
            url: '/login/get-app-tokens',
            method: 'post',
            data: {},
            dataType: 'json',
            success: function (response) {
                $('#modal-token').modal('show');
                if ( response.status && response.token ) {
                    let html = '';
                    $('#no-token-warning').hide();
                    $('#token-list').show();
                    
                    if( response.token.length == 0 ) {
                        $('#no-token-warning').show();
                    }
                    else {
                        response.token.forEach( function(token) {
                            html += '';
                            html += '    Pedido '+token.ped_codigo +'
';
                            html += '    ';
                            html += '    '+token.token+'';
                            html += '';
                        });
                    }
                    $('#token-list').html(html);
                } else {
                    $('#no-token-warning').show();
                    $('#token-list').hide();
                }
            },
            error: function () {
                $('#modal-token').modal('hide');
            }
        });
        return false;
    },
	getPendingFacialOrders: function() {
		$.ajax({
            url: '/login/get-pending-facial-orders',
            method: 'get',
            data: {},
            dataType: 'json',
            success: function (response) {
                // Retornou os códigos dos pedidos
                if ( response.status && response.orders ) {
                    console.log(response)
					var modal = new Modal({
                        title: 'Ingressos sem facial cadastrada',
                        body : ''+'[verificar layout]'+'
',
                        buttonOK : false,
                        cancelText: 'Fechar'
                    });
                    
                    modal.show();
				}
			}
		});
		return false;
	},
}
var login = new login();